package billings.classtasks.tasks;

import java.util.ArrayList;
import java.util.List;

import billings.classtasks.BaseDAO;
import billings.classtasks.DatabaseHelper;
import billings.classtasks.classes.Class;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class TaskDAO extends BaseDAO{

	//Database fields		
	private String[] allColumns = { DatabaseHelper.TASK_COLUMN_ID,
				                    DatabaseHelper.TASK_TITLE,
				                    DatabaseHelper.TASK_DESCRIPTION,
				                    DatabaseHelper.TASK_DUE_DATE,
				                    DatabaseHelper.CLASS_ID_FK };
		
		
	public TaskDAO(Context context) {
		super(context);
	}	
	
	public Task createTask(String title, String description, String dueDate, int classId){
		
		//setup content values
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.TASK_TITLE, title);
		values.put(DatabaseHelper.TASK_DESCRIPTION, description);
		values.put(DatabaseHelper.TASK_DUE_DATE, dueDate);
		values.put(DatabaseHelper.CLASS_ID_FK, classId);
		
		long insertId = database.insert(DatabaseHelper.TASK_TABLE, null, values);
		Cursor cursor = database.query(DatabaseHelper.TASK_TABLE, allColumns, 
				                       DatabaseHelper.TASK_COLUMN_ID + " = " + insertId, 
				                       null, null, null, null);
		
		cursor.moveToFirst();		
		Task newTask = cursorToTask(cursor);
		cursor.close();
		
		return newTask;
	}
	
	public Task createTask(String title, String dueDate, int classId){
		//setup content values
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.TASK_TITLE, title);
		values.put(DatabaseHelper.TASK_DUE_DATE, dueDate);
		values.put(DatabaseHelper.CLASS_ID_FK, classId);
		
		long insertId = database.insert(DatabaseHelper.TASK_TABLE, null, values);
		Cursor cursor = database.query(DatabaseHelper.TASK_TABLE, allColumns, 
				                       DatabaseHelper.TASK_COLUMN_ID + " = " + insertId, 
				                       null, null, null, null);
		
		cursor.moveToFirst();		
		Task newTask = cursorToTask(cursor);
		cursor.close();
		
		return newTask;
	}
	
	public int updateTask(long taskId, String taskDescription, String taskTitle, String taskDueDate, long taskClassId){
		long id = taskId;
		ContentValues cvs = new ContentValues();
		cvs.put(DatabaseHelper.TASK_DESCRIPTION, taskDescription);
		cvs.put(DatabaseHelper.TASK_TITLE, taskTitle);
		cvs.put(DatabaseHelper.TASK_DUE_DATE, taskDueDate);
		cvs.put(DatabaseHelper.CLASS_ID_FK, taskClassId);
		
		int rowsUpdated = database.update(DatabaseHelper.TASK_TABLE, cvs, DatabaseHelper.TASK_COLUMN_ID + " = " + id, null);
		
		return rowsUpdated;
	}
	
	public void deleteTask(long taskId){
		int ret = database.delete(DatabaseHelper.TASK_TABLE, DatabaseHelper.TASK_COLUMN_ID + " = " + taskId, null);
		if(ret > 0){
			System.out.println("Task deleted with id: " + taskId);
		}else{
			System.out.println("NO Task was deleted with id: " + taskId);
		}
	}
	
	public List<Task> getAllTasks(){
		List<Task> tasks = new ArrayList<Task>();
		
		Cursor cursor = database.query(DatabaseHelper.TASK_TABLE, allColumns, null, null, null, null, null);
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			Task task = cursorToTask(cursor);
			tasks.add(task);
			cursor.moveToNext();
		}
		
		cursor.close();
		return tasks;
	}
	
	private Task cursorToTask(Cursor cursor){
		Task task = new Task();
		task.setId(cursor.getInt(0));
		task.setTitle(cursor.getString(1));
		task.setDescription(cursor.getString(2));
		task.setDueDate(cursor.getString(3));
		task.setClassId(cursor.getInt(4));
		return task;
	}
}
